home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 4.iso / src / exampleCode / inventor / SpaceballViewer / SoSceneViewer.h < prev    next >
Encoding:
C/C++ Source or Header  |  1994-08-02  |  16.2 KB  |  491 lines

  1. /*
  2.  * Copyright (c) 1990-94 Silicon Graphics, Inc.
  3.  *
  4.  * Permission to use, copy, modify, distribute, and sell this software and
  5.  * its documentation for any purpose is hereby granted without fee, provided
  6.  * that the name of Silicon Graphics may not be used in any advertising or
  7.  * publicity relating to the software without the specific, prior written
  8.  * permission of Silicon Graphics.
  9.  *
  10.  * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND,
  11.  * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
  12.  * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
  13.  *
  14.  * IN NO EVENT SHALL SILICON GRAPHICS BE LIABLE FOR ANY SPECIAL, INCIDENTAL,
  15.  * INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND, OR ANY DAMAGES WHATSOEVER
  16.  * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER OR NOT ADVISED OF THE
  17.  * POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF LIABILITY, ARISING OUT OF OR IN
  18.  * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  19.  */
  20. //  -*- C++ -*-
  21.  
  22. /*
  23.  * Copyright (C) 1990-93   Silicon Graphics, Inc.
  24.  *
  25.  _______________________________________________________________________
  26.  ______________  S I L I C O N   G R A P H I C S   I N C .  ____________
  27.  |
  28.  |   $Revision: 1.1024 $
  29.  |
  30.  |   Classes    : SoSceneViewer
  31.  |
  32.  |   Author(s)    : Thad Beier, Alain Dumesny, David Mott, Paul Isaacs
  33.  |
  34.  ______________  S I L I C O N   G R A P H I C S   I N C .  ____________
  35.  _______________________________________________________________________
  36.  */
  37.  
  38. #ifndef  _SCENE_VIEWER_
  39. #define  _SCENE_VIEWER_
  40.  
  41. #include <X11/Intrinsic.h>
  42. #include <Xm/Xm.h>
  43. #include <Xm/RowColumn.h>
  44.  
  45. #include <Inventor/nodes/SoNode.h>
  46. #include <Inventor/SbPList.h>
  47. #include <Inventor/nodes/SoEventCallback.h>
  48. #include <Inventor/nodes/SoPerspectiveCamera.h>
  49. #include <Inventor/actions/SoGLRenderAction.h>    // transparency levels
  50. #include <Inventor/actions/SoCallbackAction.h>
  51. #include <Inventor/Xt/viewers/SoXtFullViewer.h>
  52. #include <Inventor/Xt/devices/SoXtSpaceball.h>
  53.  
  54. #include "MySlider.h"
  55.  
  56. // components
  57. class    MyColorEditor;
  58. class    SoXtMaterialEditor;
  59. class    SoXtPrintDialog;
  60. class    SoXtTransformSliderSet;
  61. class    SoXtClipboard;
  62. class    SoXtDirectionalLightEditor;
  63.  
  64. // interaction
  65. class    SoBoxHighlightRenderAction;
  66. class    SoSelection;
  67. class    SoPickedPoint;
  68.  
  69. // nodes
  70. class    SoDirectionalLight;
  71. class    SoEnvironment;
  72. class    SoGroup;
  73. class    SoLabel;
  74. class    SoMaterial;
  75. class    SoSceneViewer;
  76. class   SoSwitch;
  77. class    SoLight;
  78. class    SoTransform;
  79.  
  80. // misc.
  81. class    SvManipList;
  82.  
  83. enum SvEViewer {
  84.     SV_VWR_EXAMINER = 0, 
  85.     SV_VWR_FLY, 
  86.     SV_VWR_WALK, 
  87.     SV_VWR_PLANE, 
  88. };
  89.  
  90. enum SvEManipMode {
  91.     SV_NONE,                         // None
  92.     SV_TRACKBALL,                    // Trackball manip
  93.     SV_HANDLEBOX,                    // Handlebox manip
  94.     SV_JACK,                         // Jack manip
  95.     SV_CENTERBALL,                   // Centerball manip
  96.     SV_XFBOX,                        // TransformBox manip
  97.     SV_TABBOX                        // TabBox manip
  98. };
  99.  
  100. struct    SoSceneViewerData;
  101. struct    SvLightData;
  102.  
  103. //////////////////////////////////////////////////////////////////////////////
  104. //
  105. //  Class: SoSceneViewer
  106. //
  107. //      The SceneViewer component lets you select between different viewers
  108. //  (Examiner, Navigator,..) and lets you perform  some simple attribute
  109. //  assignment, and some manipulation of data.
  110. //
  111. //////////////////////////////////////////////////////////////////////////////
  112.  
  113. class SoSceneViewer : public SoXtComponent {
  114.  
  115.   public:    
  116.     // Constructor:
  117.     // envFile is the environment file to use. This is where the camera,
  118.     // environment node, and lights are found. 
  119.     // Passing NULL means use the default environment.
  120.     //
  121.     // Save will save all the children of root, not including root itself.
  122.     // Save Environment will save the lights and camera environment into envFile
  123.     //
  124.     SoSceneViewer(
  125.     Widget parent = NULL, 
  126.     const char *name = NULL, 
  127.     SbBool buildInsideParent = TRUE, 
  128.     SoSelection *root = NULL,
  129.     const char *envFile = NULL);
  130.     ~SoSceneViewer();
  131.  
  132.   protected:  
  133.     // This constructor takes a boolean whether to build the widget now.
  134.     // Subclasses can pass FALSE, then call SoSceneViewer::buildWidget()
  135.     // when they are ready for it to be built.
  136.     SoEXTENDER
  137.     SoSceneViewer(
  138.         Widget parent,
  139.         const char *name, 
  140.         SbBool buildInsideParent, 
  141.         SoSelection *root,
  142.         const char *envFile, 
  143.         SbBool buildNow);
  144.         
  145.  
  146.   public:
  147.     float        transformScaleFactor;
  148.     float        average;
  149.     //
  150.     // Camera operation routines
  151.     //
  152.     void    viewAll()        
  153.             { if (currentViewer != NULL) currentViewer->viewAll(); }
  154.     void           viewSelection();
  155.     void    saveHomePosition()  
  156.             { if (currentViewer != NULL) currentViewer->saveHomePosition(); }
  157.     void        setCamera(SoCamera *cam) 
  158.             { if (currentViewer != NULL) currentViewer->setCamera(cam); }
  159.     // NOTE: because the camera may be changed dynamically (switch between ortho
  160.     // and perspective), the user shouldn't cache the camera.
  161.     SoCamera    *getCamera()        
  162.             { return (currentViewer) ? currentViewer->getCamera() : NULL; }
  163.     
  164.     //
  165.     // Before new data is sent to the viewer, the newData method should
  166.     // be called to disconnect all manipulators and highlights
  167.     //
  168.     void            newData();
  169.     
  170.     // Show/hide the pulldown menu bar (default shown)
  171.     void        showMenu(SbBool onOrOff);
  172.     SbBool        isMenuShown()        { return showMenuFlag; }
  173.     
  174.     // Show/hide the viewer component trims (default shown)
  175.     void                setDecoration(SbBool onOrOff)    
  176.             { if (currentViewer != NULL) currentViewer->setDecoration(onOrOff); }
  177.     SbBool              isDecoration()        
  178.             { return currentViewer != NULL ? currentViewer->isDecoration() : FALSE; }
  179.     
  180.     // Show/hide headlight (default on) and get to the headlight node.
  181.     void                setHeadlight(SbBool onOrOff)    
  182.             { if (currentViewer != NULL) currentViewer->setHeadlight(onOrOff); }
  183.     SbBool              isHeadlight()        
  184.             { return currentViewer != NULL ? currentViewer->isHeadlight() : FALSE; }
  185.     SoDirectionalLight    *getHeadlight()        
  186.             { return currentViewer != NULL ? currentViewer->getHeadlight() : NULL; }
  187.     
  188.     //
  189.     // Sets/gets the current drawing style in the main view - The user
  190.     // can specify the INTERACTIVE draw style (draw style used when 
  191.     // the scene changes) independently from the STILL style.
  192.     //
  193.     // (default VIEW_AS_IS for both STILL and INTERACTIVE)
  194.     //
  195.     void            setDrawStyle(SoXtViewer::DrawType type, 
  196.                 SoXtViewer::DrawStyle style)
  197.                 { currentViewer->setDrawStyle(type, style); }
  198.     SoXtViewer::DrawStyle getDrawStyle(SoXtViewer::DrawType type)
  199.                 { return currentViewer->getDrawStyle(type); }
  200.     
  201.     //
  202.     // Sets/gets the current buffering type in the main view
  203.     // (default BUFFER_INTERACTIVE on Indigo, BUFFER_DOUBLE otherwise)
  204.     //
  205.     void                setBufferingType(SoXtViewer::BufferType type)
  206.                 { currentViewer->setBufferingType(type); }
  207.     SoXtViewer::BufferType getBufferingType()
  208.                 { return currentViewer->getBufferingType(); }
  209.  
  210.     // Turn viewing on/off (Default to on) in the viewers.
  211.     void                setViewing(SbBool onOrOff)  { currentViewer->setViewing(onOrOff); }
  212.     SbBool              isViewing()    { return currentViewer->isViewing(); }
  213.     
  214.     // Set/get the level of transparency type
  215.     void        setTransparencyType(SoGLRenderAction::TransparencyType type)
  216.                     { currentViewer->setTransparencyType(type); }
  217.     SoGLRenderAction::TransparencyType    getTransparencyType()
  218.                     { return currentViewer->getTransparencyType(); }
  219.  
  220.     // returns the current render area widget
  221.     Widget        getRenderAreaWidget()    { return currentViewer->getRenderAreaWidget(); }
  222.     
  223. #ifdef EXPLORER
  224.     // set the EXPLORER user mode callback routine
  225.     void        setUserModeEventCallback(SoXtRenderAreaEventCB *fcn);
  226. #endif /* EXPLORER */
  227.  
  228.  protected:
  229.     // redefine these
  230.     virtual const char *    getDefaultWidgetName() const;
  231.     virtual const char *    getDefaultTitle() const;
  232.     virtual const char *    getDefaultIconTitle() const;
  233.  
  234.     // Support for menus in the popup planes
  235.     Widget        popupWidget;
  236.  
  237.     // Stuff to do after the component has been realized (shown)
  238.     virtual void    afterRealizeHook();
  239.     
  240.  private:
  241.     // SPACEBALL
  242.     // Gets rid of wierd lights at the beginning of a dxfToIv generated
  243.     // scene.
  244.     void        fixDXFSceneGraph(SoGroup *sceneRoot);
  245.     // The spaceball
  246.     SoXtSpaceball    *spaceball;
  247.     // Routine for initialization of the spaceball
  248.     void        setupSpaceball();
  249.     // Spaceball event Callback
  250.     static void my3DeventCB(void *userData, SoEventCallback *);
  251.     // xform node to be used when nothing is selected.
  252.     SoTransform *xform;  // Transform node used by my3DeventCB if
  253.                          // there is no part selected
  254.     // Slider for setting spaceball translation speed
  255.     MySlider        *transformScaleSlider;
  256.     // Routine that builds the slider window
  257.     void        createTransformScaleEditor();
  258.     // Routine that updates spaceball translate speed.
  259.     static void        transformScaleCallback(void *userData, float val);
  260.  
  261.     // Scene graph data
  262.     SoSelection            *selection;            // the same user supplied graph
  263.     SoSeparator     *sceneGraph;        // user supplied scene graph
  264.     
  265.     // Lights, camera, environment!
  266.     SoGroup             *lightsCameraEnvironment;
  267.     SoLabel             *envLabel;
  268.     SoEnvironment       *environment;
  269.     SoGroup             *lightGroup;
  270.     
  271.     void            createLightsCameraEnvironment();
  272.     
  273.     // Selection highlight
  274.     SoBoxHighlightRenderAction *highlightRA;
  275.     
  276.     //
  277.     // Widgets and menus
  278.     //
  279.     
  280.     Widget        mgrWidget;  // our topmost form widget
  281.     Widget        menuWidget; // topbar menu widget
  282.     SbBool        showMenuFlag;
  283.     SoSceneViewerData    *menuItems; // list of menu items data
  284.     
  285.     Widget         buildWidget(Widget parent);
  286.     void        buildAndLayoutMenu(Widget parent);
  287.     void        buildAndLayoutViewer(SoXtFullViewer *vwr);
  288.  
  289.     // callback for all menu buttons to perform action
  290.     static void            processTopbarEvent(Widget, SoSceneViewerData *,
  291.                    XmAnyCallbackStruct *);
  292.     // callback when a menu is displayed
  293.     static void        menuDisplay(Widget, SoSceneViewerData *, XtPointer);
  294.     
  295.     // this is called after objects are added/deleted or the selection changes
  296.     void        updateCommandAvailability();
  297.     
  298.     //
  299.     // File
  300.     //
  301.     
  302.     // file reading methods
  303.     int        fileMode;
  304.     char    *fileName;
  305.     SbBool    useShowcaseBrowser;
  306.     void    getFileName();
  307.     void    doFileIO(const char *filename);
  308.     SbBool        readFile(const char *filename);
  309.     SbBool    writeFile(const char *filename);
  310.     SbBool    readEnvFile(const char *filename);
  311.     SbBool    writeEnvFile(const char *filename);
  312.     void    deleteScene();
  313.     void    save();
  314.     void    removeCameras(SoGroup *root);
  315.     
  316.     // motif vars for file reading/writting
  317.     Widget        fileDialog;
  318.     static void         fileDialogCB(Widget, SoSceneViewer *,
  319.                                     XmFileSelectionBoxCallbackStruct *);
  320.  
  321.     // motif vars for texture editing
  322.     Widget              textureDialog;
  323.     static void         textureDialogCB(Widget, SoSceneViewer *,
  324.                                     XmFileSelectionBoxCallbackStruct *);
  325.     void        editTexture();
  326.         
  327.     // printing vars
  328.     SoXtPrintDialog       *printDialog;
  329.     void                print();
  330.     static void         beforePrintCallback(void *uData, SoXtPrintDialog *);
  331.     static void         afterPrintCallback(void *uData, SoXtPrintDialog *);
  332.  
  333.  
  334.     SbBool        feedbackShown;
  335.     
  336.     void        showAboutDialog();
  337.     
  338.     //
  339.     // Edit
  340.     //
  341.     
  342.     // Select parent, if there is one; select everything.
  343.     void        pickParent();
  344.     void        pickAll();
  345.  
  346.     // for copy and paste
  347.     SoXtClipboard    *clipboard;     // copy/paste 3d data
  348.     void        destroySelectedObjects();
  349.  
  350.     // Paste callback - invoked when paste data transfer is complete
  351.     void                pasteDone(SoPathList *pathList);
  352.     static void        pasteDoneCB(void *userData, SoPathList *pathList);
  353.     
  354.     
  355.     //
  356.     // Viewing
  357.     //
  358.     
  359.     SoXtFullViewer    *viewerList[4];    // list of viewers
  360.     SvEViewer        whichViewer;    // current viewer ID
  361.     SoXtFullViewer    *currentViewer;    // current viewer pt
  362.     
  363.     void                switchToViewer(SvEViewer newViewer);
  364.     
  365.     // Environment: fog, antialiasing
  366.     SbBool        fogFlag;        // Fog on/off
  367.     void        setFog(SbBool onOrOff);    // Turns fog on/off
  368.     SbBool        antialiasingFlag;    // AA-ing on/off
  369.     void        setAntialiasing(SbBool onOrOff); // Turns AA-ing on/off
  370.     
  371.     // Background color
  372.     MyColorEditor       *backgroundColorEditor;
  373.     const SbColor &    getBackgroundColor()    { return currentViewer->getBackgroundColor(); }
  374.     void        editBackgroundColor();    // Invokes color editor on bkg
  375.     static void            backgroundColorCallback(void *userData,
  376.                 const SbColor *color);
  377.     
  378.     //
  379.     // Editors
  380.     //
  381.     
  382.     SbBool              ignoreCallback;
  383.     SoMaterial         *findMaterialForAttach( const SoPath *target );
  384.     SoPath             *findTransformForAttach( const SoPath * target );
  385.     // callback used by Accum state action created by findMaterialForAttach
  386.     static SoCallbackAction::Response findMtlPreTailCB(void *data, 
  387.                         SoCallbackAction *accum,
  388.                         const SoNode *);
  389.     
  390.     // transform slider set
  391.     SoXtTransformSliderSet *transformSliderSet;
  392.     void        createTransformSliderSet();
  393.  
  394.     // Material editor
  395.     SoXtMaterialEditor      *materialEditor;
  396.     void                createMaterialEditor();
  397.     
  398.     // Color editor
  399.     MyColorEditor       *colorEditor;
  400.     void        createColorEditor();
  401.  
  402.  
  403.     //
  404.     // Manips
  405.     //
  406.     SvEManipMode curManip;
  407.     SbBool       curManipReplaces;
  408.     SvManipList     *maniplist;    // list of selection/manip/xfPath triplets.
  409.  
  410.     // replaces manips with the given type for all selected objects.
  411.     void    replaceAllManips( SvEManipMode manipType );
  412.  
  413.     // attaches a manipulator
  414.     void    attachManip( SvEManipMode manipType, SoPath *p );
  415.     void    attachManipToAll( SvEManipMode manipType );
  416.  
  417.     // detaches a manipulator
  418.     void    detachManip( SoPath *p );
  419.     void    detachManipFromAll();
  420.  
  421.     // Temporarily remove manips for writing, printing, copying, etc.
  422.     // and restore later
  423.     void    removeManips();
  424.     void    restoreManips();
  425.  
  426.     // Callback to adjust size of scale tabs. Used only for SoTabBoxManip
  427.     // Added to viewer as a finishCallback.
  428.     static void SoSceneViewer::adjustScaleTabSizeCB( void *, SoXtViewer *);
  429.  
  430.     //
  431.     // Lighting
  432.     //
  433.     
  434.     SbPList        lightDataList;
  435.     SoXtDirectionalLightEditor *headlightEditor;
  436.     SvLightData        *headlightData;
  437.     void        addLight(SoLight *light);
  438.     SvLightData        *addLightEntry(SoLight *light, SoSwitch *lightSwitch);
  439.     void        addLightMenuEntry(SvLightData *);
  440.     void        turnLightOnOff(SvLightData *data, SbBool flag);
  441.     static void        lightToggleCB(Widget, SvLightData *, void *);
  442.     void        editLight(SvLightData *data, SbBool flag);
  443.     static void        editLightToggleCB(Widget, SvLightData *, void *);
  444.     static void        editLightColorCB(Widget, SvLightData *, void *);
  445.     void        removeLight(SvLightData *);
  446.     static void        removeLightCB(Widget, SvLightData *, void *);
  447.     static void        lightSubmenuDisplay(Widget, SvLightData *, void *);
  448.     
  449.     // vars to make the light manips all the same size
  450.     float        lightManipSize;
  451.     SbBool        calculatedLightManipSize;
  452.     
  453.     // temporary remove/add the light manip geometry of the attached manips
  454.     // (used for file writting and printing)
  455.     void        removeAttachedLightManipGeometry();
  456.     void        addAttachedLightManipGeometry();
  457.     
  458.     // Ambient lighting color
  459.     MyColorEditor       *ambientColorEditor;
  460.     void        editAmbientColor();    // Invokes color editor on amb
  461.     static void            ambientColorCallback(void *userData,
  462.                 const SbColor *color);
  463.     
  464.     //
  465.     // Selection
  466.     //
  467.     // manages changes in the selection.
  468.     static void     deselectionCallback(void *userData, SoPath *obj);
  469.     static void     selectionCallback(void *userData, SoPath *obj);
  470.     static SoPath *    pickFilterCB(void *userData, const SoPickedPoint *pick);
  471.     
  472.     //
  473.     // Convenience routines
  474.     //
  475.     static SbBool    isAffectedByTransform( SoNode *node );
  476.     static SbBool    isAffectedByMaterial( SoNode *node );
  477.  
  478. #ifdef EXPLORER
  479.     //
  480.     // user pick function
  481.     //
  482.     SoXtRenderAreaEventCB   *userModeCB;
  483.     void            *userModedata;
  484.     SbBool            userModeFlag;
  485. #endif /* EXPLORER */
  486.  
  487.     void constructorCommon(SoSelection *root, const char *envFile, SbBool buildNow);
  488. };
  489.  
  490. #endif  /* _SCENE_VIEWER_ */
  491.